Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #173 +/- ##
=========================================
Coverage 64.93% 64.93%
+ Complexity 981 980 -1
=========================================
Files 48 48
Lines 3094 3094
=========================================
Hits 2009 2009
Misses 1085 1085 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
I'm not a big fan of this – the kanji mode exists specifically to massively reduce the amount of storage required for that. Looking at the original zxing code I noticed that they subtract 1 from the length to make it uneven, which we don't do. Could you test locally if that makes a difference for your result? |
|
I also tried to generate the same QR code with ZXing, which yielded a valid QR code, so there must be some bug in Bacon, but I can't tell, which. |
|
You can open the link yourself :) |
My bad😅 (I thought you are testing it in locally.) It works with "あいうえお" in ZXing. So yeah, I think something wrong with Bacon but I'm not sure what is. |
|
I'd recommend to poke around and compare the code paths for kanji between Bacon and ZXing, it might be some very small knob. |
|
I made an attempt to find the mistake, but haven't succeeded so far.
That int maxI = bytes.length - 1; // bytes.length must be even
for (int i = 0; i < maxI; i += 2) {Since we're sure that int maxI = bytes.length;
for (int i = 0; i < maxI; i += 2) {
$bytes = iconv('utf-8', 'SHIFT-JIS', 'あいうえお');
print_r(unpack('C*', $bytes));
$bytes = mb_convert_encoding('あいうえお', 'SHIFT-JIS');
print_r(unpack('C*', $bytes));Both result in the same expected output: Array
(
[1] => 130
[2] => 160
[3] => 130
[4] => 162
[5] => 130
[6] => 164
[7] => 130
[8] => 166
[9] => 130
[10] => 168
)
|
|
I managed to get a working QR code by replacing BaconQrCode/src/Encoder/Encoder.php Line 118 in 1e00ebd This definitely warrants deeper analysis and understanding, but this is the culprit. |
|
Oh, great find, that does actually make a lot of sense, since the variable literally indicates number of letters, not bytes! Though we are still using iconv in this library, as it's more widely installed, so |
Fixes unreadable QR codes in Kanji mode with Shift-JIS encoding (see Bacon#172). PR Bacon#173 worked around the issue by forcing Byte mode, but that lost Kanji mode’s efficiency. The actual cause was using strlen() to count characters. Replacing it with iconv_strlen($content, 'utf-8') ensures correct character count and restores proper Kanji encoding.
|
Superseded by #200 |
Fixes unreadable QR codes in Kanji mode with Shift-JIS encoding (see Bacon#172). PR Bacon#173 worked around the issue by forcing Byte mode, but that lost Kanji mode’s efficiency. The actual cause was using strlen() to count characters. Replacing it with iconv_strlen($content, 'utf-8') ensures correct character count and restores proper Kanji encoding.
* Fix Kanji mode QR code generation Fixes unreadable QR codes in Kanji mode with Shift-JIS encoding (see #172). PR #173 worked around the issue by forcing Byte mode, but that lost Kanji mode’s efficiency. The actual cause was using strlen() to count characters. Replacing it with iconv_strlen($content, 'utf-8') ensures correct character count and restores proper Kanji encoding. * Test: Cover exclusive vs. mixed Shift-JIS data for Kanji mode * Use strlen() for numeric and alphanumeric modes to avoid iconv overhead For NUMERIC and ALPHANUMERIC modes, strlen() is sufficient since these modes only operate on single-byte characters. strlen() runs in O(1) time and avoids the overhead of iconv_strlen(). * Rewrite appendBytes() using a PHP match Nicer, and more consistent with some other code in the encode() method. Invalid modes are really not expected here, as this private method is called only by encode(), and the mode is determined right at the beginning of encode() by calling chooseMode(). Though, if an invalid mode ever comes here, the match would throw an UnhandledMatchError.

This PR is fix for the issue #172